package com.wans.guli.product.service.impl;

import com.wans.guli.product.entity.BrandEntity;
import com.wans.guli.product.entity.CategoryEntity;
import com.wans.guli.product.service.BrandService;
import com.wans.guli.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wans.guli.common.utils.PageUtils;
import com.wans.guli.common.utils.Query;

import com.wans.guli.product.dao.CategoryBrandRelationDao;
import com.wans.guli.product.entity.CategoryBrandRelationEntity;
import com.wans.guli.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> queryPageByBrandId(Long brandId) {

        return this.lambdaQuery().eq(CategoryBrandRelationEntity::getBrandId, brandId).list();
    }

    @Override
    public void saveCategoryBrand(CategoryBrandRelationEntity categoryBrandRelation) {
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        boolean save = this.save(categoryBrandRelation);

        if (!save) {
            throw new RuntimeException("保存失败");
        }

    }

    /**
     * 根据分类id查询品牌
     *
     * @param catId 分类id
     * @return 品牌
     */
    @Override
    public List<BrandEntity> listBrands(Long catId) {
        // 查询分类关联品牌id
        List<CategoryBrandRelationEntity> brandRelationEntities = this.lambdaQuery()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId).list();
        if (brandRelationEntities.isEmpty())
            // 该分类没有关联品牌
            return Collections.emptyList();
        // 查询品牌
        List<BrandEntity> brandEntities = brandService.listByIds(
                brandRelationEntities.stream()
                        .map(CategoryBrandRelationEntity::getBrandId)
                        .collect(Collectors.toList()
                        )
        );

        return brandEntities;
    }

}